package com.leyou.upload.web;

import com.leyou.upload.service.UploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

@RestController
public class UploadController {

    @Autowired
    private UploadService uploadService;

    /**
     * 图片上传
     * @param multipartFile     多部件file
     * @return      返回图片的访问路径
     */
    @PostMapping("/image")
    public ResponseEntity<String> upload(@RequestParam("file")MultipartFile multipartFile){
        String url = uploadService.upload(multipartFile);
        return ResponseEntity.ok(url);
    }

    /**
     * 获取上传到阿里云的前面
     * @return  返回值是一个map结构： 返回格式如下：

    {"accessId":"LTAIUVK6tAM6xxYm",
    "policy":"eyJleHBpcmF0aW9uIjoiMjAxOS0wNy0yNlQwOTo1NzozMS43NjhaIiwiY29uZGl0aW9ucyI6W1siY29udGVudC1sZW5ndGgtcmFuZ2UiLDAsMTA0ODU3NjAwMF0sWyJzdGFydHMtd2l0aCIsIiRrZXkiLCIiXV19",
    "signature":"7HT324IqlH8ghxsmOgoMcy1scDI=",
    "dir":"",
    "host":"http://leyou-shunyi.oss-cn-beijing.aliyuncs.com",
    "expire":"1564135051768"
    }
     */
    @GetMapping("/signature")
    public ResponseEntity<Map<String,Object>> signature(){
        return ResponseEntity.ok(uploadService.signature());
    }
}
